package com.risetechnology.krystalkitchen1;

import android.app.Activity;
import android.app.Dialog;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class DialogReminder extends Activity{
	private MediaPlayer mp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String name = getIntent().getStringExtra("SCHEDULE_NAME");
		String time = getIntent().getStringExtra("SCHEDULE_TIME");
		
		
		Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
		if (alert == null) {
			// alert is null, using backup
			alert = RingtoneManager
					.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
			if (alert == null) { // I can't see this ever being null (as always
									// have a default notification) but just
									// incase
				// alert backup is null, using 2nd backup
				alert = RingtoneManager
						.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
			}
		}
		
		mp = MediaPlayer.create(this, alert);
		mp.setLooping(true);
		mp.start();
		
		showReminderDialog(name + " at " + time);
	}
	
	private void showReminderDialog(String content) {
		final Dialog dialog = new Dialog(this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_reminder);

		Window window = dialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();

		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.FILL_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);

		dialog.getWindow().setAttributes(wlp);
		dialog.setCancelable(false);
		dialog.show();

		
		TextView tv = (TextView) dialog.findViewById(R.id.tv_content);
		tv.setText(content);

		Button buttonOK = (Button) dialog.findViewById(R.id.btn_ok);
		buttonOK.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();
				mp.stop();
				mp.release();
				finish();
			}
		});
	}
}
